package us.yesor.atlast.experimental;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import us.yesor.atlast.InteractionTable;
import us.yesor.atlast.ItemInteraction;
import us.yesor.atlast.R;
import us.yesor.atlast.framework.AtLastDbAdapter;
import us.yesor.atlast.framework.ContentProviderAdapter;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.text.format.Time;
import android.util.Log;

public class SampleDataLoader {

	public static void loadSampleData(AtLastDbAdapter adapter) {
		adapter.open();
		adapter.overWrite();
        initializeInteractions(adapter);
	}
	
	public static void loadSampleData(Activity activity) {
        initializeInteractions(activity);
	}

	private static void initializeInteractions(AtLastDbAdapter adapter) {
		Context ctx = adapter.getContext();
		XmlResourceParser parser = ctx.getResources().getXml(
				R.xml.interactions);
		try {
			processSeedInteractions(adapter, parser);
		} catch (Exception e) {
			Log.e("DEBUG_TAG", "Failed to parse the interactions.xml table");
		}
		
	}
	
	private static void initializeInteractions(Activity activity) {
		ContentResolver resolver = activity.getContentResolver();
		XmlResourceParser parser = activity.getResources().getXml(
				R.xml.interactions);
		try {
			processSeedInteractions(resolver, parser);
		} catch (Exception e) {
			Log.e("DEBUG_TAG", "Failed to parse the interactions.xml table");
		}
		
	}

	private static boolean processSeedInteractions(AtLastDbAdapter dbAdapter, XmlResourceParser parser)
			throws XmlPullParserException, IOException {
		int eventType = -1;
		boolean foundInteractions = false;

		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {
				String tagName = parser.getName();
				if (tagName.equals("interaction")) {
					foundInteractions = true;
					String collection = parser.getAttributeValue(null,"collection");
					String item = parser.getAttributeValue(null, "item");
					String trigger = parser.getAttributeValue(null, "trigger");
					String value = parser.getAttributeValue(null, "value");
					String state = parser.getAttributeValue(null, "state");
					Time now = new Time();
					now.setToNow();
					String timeString = now.format2445();
					ItemInteraction interaction = new ItemInteraction(collection, item, trigger, value, state, timeString);
					dbAdapter.insertInteraction(interaction);
				}
			}
			eventType = parser.next();
		}
		if (foundInteractions == false) {
			return false;
		} else
			return true;
	}

	private static boolean processSeedInteractions(ContentResolver resolver, XmlResourceParser parser)
			throws XmlPullParserException, IOException {
		int eventType = -1;
		boolean foundInteractions = false;

		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {
				String tagName = parser.getName();
				if (tagName.equals("interaction")) {
					foundInteractions = true;
					String collection = parser.getAttributeValue(null,"collection");
					String item = parser.getAttributeValue(null, "item");
					String trigger = parser.getAttributeValue(null, "trigger");
					String value = parser.getAttributeValue(null, "value");
					String state = parser.getAttributeValue(null, "state");
					Time now = new Time();
					now.setToNow();
					String timeString = now.format3339(false);
					ItemInteraction interaction = new ItemInteraction(collection, item, trigger, value, state, timeString);
					resolver.insert(InteractionTable.CONTENT_URI, ContentProviderAdapter.getFormattedContentValues(interaction));
				}
			}
			eventType = parser.next();
		}
		if (foundInteractions == false) {
			return false;
		} else
			return true;
	}

}
